package org.dyndns.opendemogroup.hadoopoptimizer;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;

public class DistributedRunDriver
{

	public static void main ( String[] args ) throws IOException
	{
		JobClient client = new JobClient ( );
		JobConf conf = new JobConf ( DistributedRunDriver.class );

		// { output is <bestFitness, genome>
		conf.setOutputKeyClass ( Text.class );
		conf.setOutputValueClass ( Text.class );
		// }

		if ( args.length < 2 )
		{
			System.out
					.println ( "WARNING: Using default input and output folders." );
			args = new String[]
			{
				"input", "output"
			};
		}
		FileSystem fs = FileSystem.get ( new Configuration ( ) );

		conf.setInputPath ( new Path ( args[0] ) );
		Path outputPath = new Path ( args[1] );
		conf.setOutputPath ( outputPath );
		// { delete the contents of outputPath
		if ( fs.exists ( outputPath ) )
		{
			fs.delete ( outputPath );
		}
		// }

		conf.setMapperClass ( DistributedRunMapper.class );
		conf.setReducerClass ( DistributedRunReducer.class );
		client.setConf ( conf );
		JobClient.runJob ( conf );
	}

}
